package cn.edu.sdjzu.xg11.web;

import org.apache.log4j.Logger;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sdjzu.xg11.dao.UserInfoDao;
import cn.edu.sdjzu.xg11.domain.UserInfo;

@Controller
public class LoginController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	@Autowired
	private UserInfoDao userInfoDao;
//	请求...index.html时，运行此方法
	@RequestMapping(value="/index.html")
	public String loginPage(){
		if (logger.isDebugEnabled()) {
			logger.debug("loginPage() - start");
		}

		//返回一个逻辑视图名称，被bysj-servlet.xml包装后形成物理视图。转发到该物理视图。

		if (logger.isDebugEnabled()) {
			logger.debug("loginPage() - end");
		}
		return "login";
	}
	@RequestMapping(value="/login/checkLogin.html")
	public ModelAndView checkLogin(HttpServletRequest  request, UserInfo userInfoFromView){
		if (logger.isDebugEnabled()) {
			logger.debug("checkLogin(HttpServletRequest, UserInfo) - start");
		}

//		System.out.println("in checkLogin...");
//		System.out.println(userInfoFromView.getUsername());
		UserInfo desiredUserInfo = loginValidate(userInfoFromView.getUsername(),userInfoFromView.getPassword());
		HttpSession session = request.getSession();
//		System.out.println("desiredUserInfo="+desiredUserInfo);
		if(desiredUserInfo!=null){
			session.setAttribute("userInfo", desiredUserInfo);
			session.removeAttribute("error");
			ModelAndView returnModelAndView = new ModelAndView("main");
			if (logger.isDebugEnabled()) {
				logger.debug("checkLogin(HttpServletRequest, UserInfo) - end");
			}
			return returnModelAndView;			
		}else{			
			ModelAndView returnModelAndView = new ModelAndView("login",
					"error", "用户名或密码错误");
			if (logger.isDebugEnabled()) {
				logger.debug("checkLogin(HttpServletRequest, UserInfo) - end");
			}
			return returnModelAndView;		
		}		
	}
	private UserInfo loginValidate(String username, String password) {
		if (logger.isDebugEnabled()) {
			logger.debug("loginValidate(String, String) - start");
		}

		//get Collection<UserInfo>
		Collection<UserInfo> userInfos = new UserInfoDao().getUserInfos();
		//get the matched userInfo object
		for (UserInfo userInfo : userInfos) {
			if(username.equals(userInfo.getUsername())&&password.equals(userInfo.getPassword())){
				if (logger.isDebugEnabled()) {
					logger.debug("loginValidate(String, String) - end");
				}
				return userInfo;				
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("loginValidate(String, String) - end");
		}
		return null;
	}
	
	@RequestMapping(value="/logout.html")
	public String logOutPage(HttpServletRequest request){
		if (logger.isDebugEnabled()) {
			logger.debug("logOutPage(HttpServletRequest) - start");
		}

		//返回一个逻辑视图名称，被bysj-servlet.xml包装后形成物理视图。转发到该物理视图。
		request.getSession().removeAttribute("userInfo");
		System.out.println(request.getSession().getAttribute("userInfo"));;

		if (logger.isDebugEnabled()) {
			logger.debug("logOutPage(HttpServletRequest) - end");
		}
		return "login";
	}
}
